package com.example.controller;


import com.example.service.AccountService;
import com.example.service.impl.AccountServiceImpl;
import io.seata.core.context.RootContext;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description: 账户控制器
 * @Author: Mr_ma
 * @Date: 2021-10-31 15:56
 * @param :
 * @return: null
 **/
@RestController
@RequiredArgsConstructor
public class AccountController {

    @Autowired
    private AccountServiceImpl accountService;

    @GetMapping("account/debit")
    public String debit(@RequestParam("id") Integer id, @RequestParam("amount") Long amount){
        //模拟超时异常，全局事务回滚
        try {
            Thread.sleep(30*1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("xid:" + RootContext.getXID());
        accountService.debit(id,amount);
        return "扣减账户余额成功";
    }

}
